home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Examples / Simple / Want something easier? / Simple.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-06  |  6.4 KB  |  231 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Simple.c
  3. ///--------------------------------------------------------------------------------------
  4.  
  5. #include <SWIncludes.h>            // Automatically include all SpriteWorld headers
  6. #include <SWGameUtils.h>
  7.  
  8. #include "SWApplication.h"
  9. #include "Simple.h"
  10.  
  11. #define kMaxFPS                    30            // Keep the animation from going too fast
  12.  
  13. #define kNumSprites                10
  14. #define kMaxSpriteMoveDelta        8
  15.  
  16.  
  17. SpriteWorldPtr        gSpriteWorldP;
  18. SpriteLayerPtr        gSpriteLayerP;
  19. SpritePtr            gMasterBallSpriteP;
  20. WindowPtr            gWindowP;
  21.  
  22.  
  23. ///--------------------------------------------------------------------------------------
  24. // main
  25. ///--------------------------------------------------------------------------------------
  26.  
  27. void main(void)
  28. {
  29.     Initialize(kNumberOfMoreMastersCalls);
  30.     Randomize();
  31.  
  32.     if (SWHasSystem7())
  33.     {
  34.         SetCursor(*GetCursor(watchCursor));
  35.         
  36.         CreateWindow();
  37.         SetUpSpriteWorld();
  38.         CreateBallSprite();
  39.         
  40.         HideCursor();
  41.         
  42.         AddSprites();
  43.         RunAnimation();
  44.         CleanUp();
  45.     }
  46.     else
  47.     {
  48.         CantRunOnThisMachine();
  49.     }
  50. }
  51.  
  52.  
  53. ///--------------------------------------------------------------------------------------
  54. //    CreateWindow
  55. ///--------------------------------------------------------------------------------------
  56.  
  57. void    CreateWindow(void)
  58. {
  59.     Rect        windRect;
  60.     RgnHandle    mBarUpdateRgn;
  61.     
  62.     gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
  63.  
  64.     if (gWindowP != NULL)
  65.     {
  66.         MoveWindow(gWindowP, 0, 0, false);
  67.         windRect = gWindowP->portRect;
  68.  
  69.             // Center window in screen
  70.         CenterRect(&windRect, &qd.screenBits.bounds);
  71.         MoveWindow(gWindowP, windRect.left, windRect.top, false);
  72.  
  73.         ShowWindow(gWindowP);
  74.         SetPort(gWindowP);
  75.         mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
  76.         EraseRgn(mBarUpdateRgn);
  77.     }
  78.     else
  79.         CantFindResource();
  80. }
  81.  
  82.  
  83. ///--------------------------------------------------------------------------------------
  84. //    SetUpSpriteWorld
  85. ///--------------------------------------------------------------------------------------
  86.  
  87. void    SetUpSpriteWorld(void)
  88. {
  89.     OSErr            err;
  90.     PixPatHandle    pixPatH;
  91.  
  92.         // Initialize the SpriteWorld package
  93.     err = SWEnterSpriteWorld();
  94.     FatalError(err);
  95.     
  96.         // Create the SpriteWorld
  97.     err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP, NULL, NULL, 0);
  98.     FatalError(err);
  99.  
  100.         // Create the Sprite Layers
  101.     err = SWCreateSpriteLayer(&gSpriteLayerP);
  102.     FatalError(err);
  103.     
  104.         // Put the pieces together and lock the SpriteWorld. 
  105.         // Note: since we are locking the SpriteWorld before adding the sprites,
  106.         // we must make sure to lock each sprite individually when creating them.
  107.     SWAddSpriteLayer(gSpriteWorldP, gSpriteLayerP);
  108.     SWLockSpriteWorld(gSpriteWorldP);
  109.     
  110.         // Load the background pattern
  111.     pixPatH = GetPixPat(128);
  112.     
  113.         // Draw it in the background
  114.     if (pixPatH != NULL)
  115.     {
  116.         SWSetPortToBackground(gSpriteWorldP);
  117.         FillCRect(&gSpriteWorldP->backRect, pixPatH);
  118.         DisposePixPat(pixPatH);
  119.     }
  120.     
  121.         // Limit the frame rate to a specific speed
  122.     SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
  123. }
  124.  
  125.  
  126. ///--------------------------------------------------------------------------------------
  127. //    CreateBallSprite - load the master ball sprite and prepare it for cloning later
  128. ///--------------------------------------------------------------------------------------
  129.  
  130. void    CreateBallSprite(void)
  131. {
  132.     OSErr    err;
  133.     
  134.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBallSpriteP, NULL, 
  135.             128, 1, kFatMask);
  136.     FatalError(err);
  137.     
  138.         // Set up the sprite (remember that it must be locked!)
  139.     SWLockSprite(gMasterBallSpriteP);
  140.     SWSetSpriteMoveBounds(gMasterBallSpriteP, &gSpriteWorldP->backRect);
  141.     SWSetSpriteMoveProc(gMasterBallSpriteP, BallSpriteMoveProc);
  142. }
  143.  
  144.  
  145. ///--------------------------------------------------------------------------------------
  146. //    AddSprites - clone the master sprite, and add the clones to the SpriteWorld
  147. ///--------------------------------------------------------------------------------------
  148.  
  149. void    AddSprites(void)
  150. {
  151.     SpritePtr    newSpriteP;
  152.     short        spriteNum;
  153.     OSErr        err;
  154.     
  155.     for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
  156.     {
  157.             // Clone the master sprite. The clone doesn't need to be locked,
  158.             // since the master sprite was already locked.
  159.         err = SWCloneSprite(gMasterBallSpriteP, &newSpriteP, NULL);
  160.         FatalError(err);
  161.         
  162.         SWAddSprite(gSpriteLayerP, newSpriteP);
  163.         
  164.         SWSetSpriteLocation(newSpriteP,
  165.                 GetRandom(0, gSpriteWorldP->backRect.right-44), 
  166.                 GetRandom(0, gSpriteWorldP->backRect.bottom-44) );
  167.         
  168.         do
  169.         {
  170.             SWSetSpriteMoveDelta(newSpriteP,
  171.                     GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta), 
  172.                     GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta));
  173.         } while (newSpriteP->horizMoveDelta == 0 || newSpriteP->vertMoveDelta == 0);
  174.     }
  175. }
  176.  
  177.  
  178. ///--------------------------------------------------------------------------------------
  179. //    RunAnimation
  180. ///--------------------------------------------------------------------------------------
  181.  
  182. void    RunAnimation(void)
  183. {
  184.         // Make sure CopyBits, if used, doesn't try to colorize things
  185.     SWSetPortToWindow(gSpriteWorldP);
  186.     ForeColor(blackColor);
  187.     BackColor(whiteColor);
  188.     
  189.     SWUpdateSpriteWorld(gSpriteWorldP, true);
  190.  
  191.     while (!Button())
  192.     {
  193.         SWProcessSpriteWorld(gSpriteWorldP);
  194.         SWAnimateSpriteWorld(gSpriteWorldP);        
  195.     }
  196.     
  197.     SWShowMenuBar((WindowPtr)gWindowP);
  198. }
  199.  
  200.  
  201. ///--------------------------------------------------------------------------------------
  202. //     CleanUp - This function is not really necessary, since the system will dispose
  203. //    everything automatically when the program quits. However, if you sync the animation
  204. //    to the VBL, you must either call SWDisposeSpriteWorld, or SWSyncSpriteWorldToVBL
  205. //    with a value of false before your program quits, in order to remove the VBL task.
  206. ///--------------------------------------------------------------------------------------
  207.  
  208. void    CleanUp(void)
  209. {
  210.         // Dispose the master sprite, since it isn't included in the SpriteWorld
  211.     SWDisposeSprite(&gMasterBallSpriteP);
  212.     
  213.         // Dispose the SpriteWorld, including all sprites and layers currently in it
  214.     SWDisposeSpriteWorld(&gSpriteWorldP);
  215.     SWExitSpriteWorld();
  216.     
  217.     FlushEvents(everyEvent, 0);
  218.     InitCursor();
  219. }
  220.  
  221.  
  222. ///--------------------------------------------------------------------------------------
  223. //  BallSpriteMoveProc
  224. ///--------------------------------------------------------------------------------------
  225.  
  226. SW_FUNC void BallSpriteMoveProc(SpritePtr ballSpriteP)
  227. {    
  228.     SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
  229.     (void)SWBounceSprite(ballSpriteP);
  230. }
  231.